﻿function Widget_WidgetBox(obj)
{
   this.initializeBase(obj);
}
Widget_WidgetBox.prototype = new WidgetInstance;

Widget_WidgetBox.prototype.Edit = function()
{
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      this.Widget.InstanceEdit("s" + this.id + "_embed");
   }
   else   
   {
      alert("You do not have the required permission");
   }
}
 
 
Widget_WidgetBox.prototype.Save = function()
{
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      if (this.Setting.Embed.value.length < 1)
      {
         alert("Please enter the embed string from Widgetbox");
         this.Setting.Embed.focus();
         return;
      }
      
      var ColWidth = 0;
      var strColWidth = "";
      switch (this.Widget.obj.ColumnNo)
      {
         case 1:
            ColWidth = this.Widget.Tab.obj.PanelWidth_1;
            break;
         case 2:
            ColWidth = this.Widget.Tab.obj.PanelWidth_2;
            break;
         case 3:
            ColWidth = this.Widget.Tab.obj.PanelWidth_3;
            break;
         case 4:
            ColWidth = this.Widget.Tab.obj.PanelWidth_4;
            break;            
      }
      
      if (isNaN(ColWidth))
         return;

      var Dict = new Object();
      Dict.Embed = this.Setting.Embed.value;
      Dict.Width = this.Setting.Width.value.toLowerCase();
      Dict.Height = this.Setting.Height.value.toLowerCase();
      
      if (Dict.Width.length < 1)
      {
         this.Setting.Width.value = Dict.Width = "100%";
      }

      if (Dict.Height.length < 1)
      {
         this.Setting.Height.value = Dict.Height = "auto";
      }
      
      JSON.Widget_UpdateProperties(this.Widget.Tab.id, this.id, Dict);
      this.inEditMode = true;
      this.Edit();
      this.Refresh();
   }
}

Widget_WidgetBox.prototype.Refresh = function()
{
   var val = this.Setting.Embed.value;
   if (val && val.length > 0)
   {
      this.WidgetBoxIFrame.src = "about:blank";
      this.WidgetBoxIFrame.src = "/widgets/widgetbox/wb.aspx?id=" + this.id;
      val = this.Setting.Width.value;
      this.WidgetBoxIFrame.width = (val && val.length > 0 ? val : "100%");
      
      val = this.Setting.Height.value;
      if (!(val && val.length > 0))
      {
         this.Setting.Height.value = val = "auto";
      }
      
      if (val == "auto")
      {
         //check if this tab is a single column, if so, max to whole screen
         //otherwise, limit to 250px;
         if (this.Widget.Tab.obj.PanelWidth_1 == 100)
         {
            var height = f_clientHeight() - 120;
            WriteHistory("Auto: " + f_clientHeight() + ":" + height);
            this.WidgetBoxIFrame.width = (val && val.length > 0 ? val : "100%");
            this.WidgetBoxIFrame.height = height + "px";            
         }
         else
         {
            this.WidgetBoxIFrame.width = (val && val.length > 0 ? val : "100%");
            this.WidgetBoxIFrame.height = "250px";
         }
      }
      else
      {
         this.WidgetBoxIFrame.height = val;
      }
      
      this.WidgetDiv_BodySettings.style.display = "none";
      this.WidgetDiv_BodyContent.style.display = "";
   }
   else
   {
      this.inEditMode = false;
      this.Edit();
   }
}

Widget_WidgetBox.prototype.Load = function()
{
   this.Widget.InstanceLoad();
   
   this.WidgetDiv_BodySettings = $get('wdivb' + this.id + '_s');
   this.WidgetDiv_BodyContent = $get('wdivb' + this.id + '_c');
   
   this.Setting = new Object();
   this.Setting.Embed = $get('s' + this.id + '_embed');
   this.Setting.Width = $get('s' + this.id + '_width');
   this.Setting.Height = $get('s' + this.id + '_height');
   
   
   if (this.Widget.obj.State)
   {
      //if there is state information
/*
    var xml2 = '<?xml version="1.0"?><response><error>0</error></response>';
    var tree2 = xotree.parseXML( xml2 );        // XML source to object tree
*/      
      var xotree = new XML.ObjTree();
      var xmlDoc = xotree.parseXML(this.Widget.obj.State);
      
      //3 values, Embed, Width, Height
      this.Setting.Embed.value = xmlDoc.state.Embed;
      this.Setting.Width.value = xmlDoc.state.Width;
      this.Setting.Height.value = xmlDoc.state.Height;
   }
   
   this.WidgetDiv_Command_Edit = document.getElementById("wdivc_edit" + this.Widget.id);
   this.WidgetBoxIFrame = document.getElementById("wb" + this.Widget.id);
   
   this.Refresh();
               
   this.Widget.InstanceLoadComplete();
}